package com.budwk.app.sqfwV2.generator.centuryCitySecurity.excel;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.centuryCity.CenturyCitySecurityComponent;
import com.budwk.app.sqfwV2.dto.dto.SafeCheckTaskPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.ProblemParamDTO;
import com.budwk.app.sqfwV2.dto.image.SafeCheckTaskExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.CenturyCitySafeServiceEnum;
import com.budwk.app.sqfwV2.enums.RectificateTimeEnum;
import com.budwk.app.sqfwV2.enums.centuryCitySafe.CenturyCitySafeProblemEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * excel安全生产问题隐患整改责任清单(未完成)
 */
@Slf4j
public class SafetyProductionRiskGenerater extends BaseTemplateExcelGenerator {

    @Override
    protected String getTemplateName() {
        return "sqfwV2/centuryCitySecurity/safetyProductionRisk.xlsx";
    }

    /**
    * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
    *
    * @return 文件名名称
    */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称
        // 名称要求带上随机数。如果仅仅是一个中文名称的话，上传到对象存储会造成文件覆盖
        // 注意注意注意不用带上后缀
        return DayingRoadPdfComponent.getDictName(CenturyCitySafeServiceEnum.CENTER_CITY_KEY_LOCATION.getCode(), null);
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     * @param excelParam 已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {

    }

    /**
     * 初始化默认值
     * @return
     */
    public static Map<String, String> initProblemMap() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put(CenturyCitySafeProblemEnum.ONE.getCode(), "消防栓遮挡");  //1 消防栓是否存在遮挡、圈占现象
        map.put(CenturyCitySafeProblemEnum.TWO.getCode(), "消防栓无法使用");  //2 消防栓是否能正常使用
        map.put(CenturyCitySafeProblemEnum.THREE.getCode(), "灭火器过期");  //3 灭火器有无过期或损坏等
        map.put(CenturyCitySafeProblemEnum.FORE.getCode(), "灭火器不会使用");  //4 灭火器员工是否会使用
        map.put(CenturyCitySafeProblemEnum.FIVE.getCode(), "电气线路私搭乱接");  //5 电、气线路是否存在私搭乱接现象
        map.put(CenturyCitySafeProblemEnum.SIX.getCode(), "电气线路裸露");  //6 电、气线路是否存在裸露现象
        map.put(CenturyCitySafeProblemEnum.SEVEN.getCode(), "安全出口阻塞");  //7 安全出口是否畅通
        map.put(CenturyCitySafeProblemEnum.EIGHT.getCode(), "夹层住人");  //8 是否存在夹层住人情况
        map.put(CenturyCitySafeProblemEnum.NINE.getCode(), "生产危险品");  //9 是否生产、存储易燃易爆品
        map.put(CenturyCitySafeProblemEnum.TEN.getCode(), "未安装燃气报警器");  //10  是否安装燃气报警器
        map.put(CenturyCitySafeProblemEnum.ELEVEN.getCode(), "未规范安装燃气报警器");  //11  燃气报警器是否规范安装
        map.put(CenturyCitySafeProblemEnum.THIRTEEN.getCode(), "违规搭建");  //13  是否自行搭建楼层
        return map;
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        // 1 获取数据对象
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("SafetyProductionRiskGenerater customerPopularSheetDataParam param: {}", JSONObject.toJSONString(summaryList));
        // 2 构建参数map
        if (summaryList.size() == 0) {
            return;
        }
        Map<String, String> initMap = initProblemMap();
        List<Sqfw_formSummary_new> list = CenturyCitySecurityComponent.handleDatas(summaryList);
        for (Sqfw_formSummary_new summary : list) {
            SafeCheckTaskPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), SafeCheckTaskPdfDTO.class);
            Map<String, ProblemParamDTO> existProblemMap = roadPdfDTO.getExistProblemMap();
            if (existProblemMap == null || existProblemMap.size() == 0) {
                continue;
            }
            Map<String, ProblemParamDTO> problemMap = CenturyCitySafeProblemEnum.sortExistProblemMap(existProblemMap);
            for (Map.Entry<String, ProblemParamDTO> entry : problemMap.entrySet()) {
                Map<String, Object> paramMap = buildParamMap(initMap, entry, roadPdfDTO, summary);
                listMap.add(paramMap);
            }
        }
        CenturyCitySecurityComponent.buildNum(listMap);
        log.info("SafetyProductionRiskGenerater customerPopularSheetDataParam end listMap : {}", JSONObject.toJSONString(listMap));
    }

    private Map<String, Object> buildParamMap(Map<String, String> initMap, Map.Entry<String, ProblemParamDTO> entry, SafeCheckTaskPdfDTO roadPdfDTO, Sqfw_formSummary_new summary) {
        Map<String, Object> paramMap = new HashMap<>();
        ProblemParamDTO p = entry.getValue();
        // 隐患级别
        paramMap.put("riskLevel", DayingRoadPdfComponent.getDictName(p.getHazardLevelType(), null));
        paramMap.put("createUserName", StrUtil.formatStr(summary.getCreateUserName()));
        paramMap.put("leadUserName", "马冉");
        paramMap.put("reviewTime", "");
        paramMap.put("reviewResult", "");
        paramMap.put("problemHazards", initMap.get(entry.getKey()));
        // 复查时用户将状态变更为“已整改”，默认“复查验收时间”=filldate
        if (RectificateTimeEnum.IMMEDIATE_RECTIFICAIOTN.getCode().equals(p.getRectificateType())) {
            paramMap.put("reviewTime", LocalDateTimeUtil.format(roadPdfDTO.getInspectionDate(), DatePattern.NORM_DATETIME_PATTERN));
            paramMap.put("reviewResult", "已整改");
        }
        if (RectificateTimeEnum.DEADLINE_RECTIFICATION.getCode().equals(p.getRectificateType())) {
            paramMap.put("reviewTime", LocalDateTimeUtil.format(roadPdfDTO.getInspectionDate(), DatePattern.NORM_DATETIME_PATTERN));
            paramMap.put("reviewResult", "整改中");
        }
        return paramMap;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return null;
    }

    /**
    * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
    * @param excelParam excel已经填充的部分数据
    */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return SafeCheckTaskExcelImageConfigBO.class;
    }
}
